package com.example.feifei.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.example.feifei.utils.LogUtils;

/**
 * 自定义侧滑菜单
 * Created by feifei on 2017/11/28.
 */

public class MyScrollView extends ViewGroup {

    private Context mContext;

    private int mWidth;
    private int mHeight;
    //菜单界面比例
    private float mMenuWeight = 3.0f/5;
    private View mMenuView;//菜单界面
    private View mPriVIew;//内容界面

    private boolean mIsShowMenu;
    private Scroller mScroller;

    private OnMenuChangedListener mListener;

    public MyScrollView(Context context) {
        super(context,null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mScroller = new Scroller(mContext);

    }
    //测量大小
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
        //测量菜单内容的宽高
        int widthSpec = MeasureSpec.makeMeasureSpec((int)(mWidth * mMenuWeight), MeasureSpec.EXACTLY);
        int heightSpec = MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY);
        mMenuView.measure(widthSpec, heightSpec);
        //设置主页内容的宽高
        widthSpec = MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY);
        mPriVIew.measure(widthSpec, heightSpec);
    }
    //控制位置
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mMenuView.layout(-(int)(mWidth*mMenuWeight),0,0,mHeight);
        mPriVIew.layout(0,0,mWidth,mHeight);
    }

    //设置菜单View
    public void setMenu(View view){
        this.mMenuView = view;
        addView(mMenuView);
    }
    public void setPrimary(View primary){
      this.mPriVIew = primary;
        addView(mPriVIew);
    }
    private float mDownX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        switch (event.getAction()){
            //按下的时候
            case MotionEvent.ACTION_DOWN:
                mDownX = x;
                break;
            //抬起的时候
            case MotionEvent.ACTION_UP:
                int dis = (int)(x - mDownX);
                if(Math.abs(dis) > (mWidth * mMenuWeight / 2)){
                    if(dis > 0){  //如果>0则是向右滑动
                        showMenu();
                    }else{  //如果<0则是向左滑动
                        hideMenu();
                    }
                }
                break;
        }
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            postInvalidate();
        }
    }
    public boolean isShowMenu(){
        return mIsShowMenu;
    }
    public void showMenu(){
        if(mIsShowMenu){
            return;
        }
        mIsShowMenu = true;  //标记菜单已经显示
        int dx = (int)(mWidth * mMenuWeight);  //滑动到目标位置的距离
        mScroller.startScroll(getScrollX(), 0, -dx, 0, 500);
        if(mListener != null){
            mListener.onChanged(mIsShowMenu);
        }
        invalidate();
    }
    public void hideMenu(){
        if(!mIsShowMenu){
            return;
        }
        mIsShowMenu = false;
        int dx = (int)(mWidth * mMenuWeight);
        mScroller.startScroll(getScrollX(), 0, dx, 0, 500);
        if(mListener != null){
            mListener.onChanged(mIsShowMenu);
        }
        invalidate();
    }
    public interface OnMenuChangedListener{
        void onChanged(boolean isShow);
    }

    public void setOnMenuChangedListener(OnMenuChangedListener listener){
        mListener = listener;
    }
}
